package com.yubest.designpattern.behavior.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 天气预报
 *
 * @Author: hweiyu
 * @Date: 2022/10/26 17:22
 */
public class Weather implements Subject {

    private Region region;

    /**
     * 观察者列表
     */
    private final List<Observer> observers = new ArrayList<>();

    public void setData(Region region) {
        this.region = region;
        this.notifyObserver();
    }

    @Override
    public void resisterObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        int i = observers.indexOf(o);
        if (i >= 0) {
            observers.remove(i);
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer o : observers) {
            o.update(this.region);
        }
    }
}
